﻿using System;
using System.Data;
using System.Text;
using System.IO;
using System.Web;
using System.Data.OleDb;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using ADOX;
namespace JumbotCms.Install
{
    public partial class _step1_0 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (System.Web.HttpContext.Current.Application["CMS:BasePath"] == null)
                Response.Redirect("index.aspx");
            if (!Page.IsPostBack)
            {
                if (!Jumbot.Common.DirFile.FileExists("scripts/access/step1.sql"))
                {
                    this.DivMessage.InnerHtml = "脚本文件install/scripts/access/step1.sql不存在";
                    this.btnSave.Enabled = false;
                }
                else
                    this.txtDBPath.Text = System.Web.HttpContext.Current.Application["CMS:BasePath"].ToString() + "_data/database/db" + DateTime.Now.ToString("yyyyMMddHHmmssffff") + ".config";
            }

        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            string dbConnString = "";
            string DBPath = this.txtDBPath.Text;
            if (Jumbot.Common.DirFile.FileExists(DBPath))
                this.DivMessage.InnerHtml = "目标数据库已存在,无法创建，请指定一个不存在的路径";
            else
            {
                dbConnString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Server.MapPath(DBPath);
                Jumbot.Common.DirFile.CreateFolder(Jumbot.Common.DirFile.GetFolderPath(Server.MapPath(DBPath)));
                ADOX.CatalogClass cat = new ADOX.CatalogClass();
                cat.Create(dbConnString);
                //保存配置文件
                try
                {
                    string _connFile = Server.MapPath("~/_data/config/conn.config");
                    string _connText = "<?xml version=\"1.0\" encoding=\"utf-8\"?><Root><dbType>0</dbType><dbPath>" + DBPath + "</dbPath><dbConnStr></dbConnStr></Root>";
                    StreamWriter sw = new StreamWriter(_connFile, false, Encoding.UTF8);
                    sw.Write(_connText);
                    sw.Close();
                }
                catch (Exception ex)
                {
                    throw ex;
                }
                if (Jumbot.Common.ExecuteSqlBlock.Go("0", dbConnString, Server.MapPath("scripts/access/step1.sql")))
                    Response.Redirect("step2.aspx");
                else
                    this.DivMessage.InnerHtml = "安装失败";
            }
            System.Web.HttpContext.Current.Application.Lock();
            System.Web.HttpContext.Current.Application["Database:dbType"] = null;
            System.Web.HttpContext.Current.Application["Database:dbConnStr"] = null;
            System.Web.HttpContext.Current.Application["System:Info"] = null;
            System.Web.HttpContext.Current.Application.UnLock();
        }
    }
}
